package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.dto.CommodityInMachine;
import com.fuhe.chen.vendingmachine.dto.ItemInMachine;
import com.fuhe.chen.vendingmachine.dto.cond.CommodityCond;
import com.fuhe.chen.vendingmachine.dto.cond.ItemCond;
import com.fuhe.chen.vendingmachine.pojo.Commodity;
import com.fuhe.chen.vendingmachine.pojo.CommodityPicture;
import com.fuhe.chen.vendingmachine.pojo.CommoditySpecification;
import com.fuhe.chen.vendingmachine.pojo.Item;
import com.fuhe.chen.vendingmachine.service.ICategoryService;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.fuhe.chen.vendingmachine.service.IItemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 售货机商品管理首页
 * 新增商品/修改商品信息/删除商品信息
 */
@Controller
@Api(tags = "售货机商品信息管理")
@RequestMapping("/admin/item")
public class ItemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ItemController.class);

    @Autowired
    IItemService itemService;

    @Autowired
    ICategoryService categoryService;


    /**
     * 新增单个售货机商品信息
     */
    @ApiOperation(value = "新增售货机商品信息", notes = "新增商品为下架状态,需要手动上架")
    @PostMapping("/addItem")
    @ResponseBody
    @AddLog(operation = "新增售货机商品信息")
    public ApiResponse<String> addItem(@RequestParam("item_name") String item_name,
                                       @RequestParam("specifications") String specifications,
                                       @RequestParam("price") Float price,
                                       @RequestParam("item_category_id") Integer item_category_id,
                                       MultipartFile picture) {

        String imgPath = "#";
        Item item = new Item();
        item.setItem_category_id(item_category_id);
        item.setItem_name(item_name);
        item.setItem_status(0);
        item.setPrice(price);
        item.setSpecifications(specifications);
        item.setPicture_url(Utils.upload(picture, imgPath));

        itemService.addItem(item);

        ApiResponse<String> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.CREATE_SUCCESS_MESSAGE);

        return response;
    }

    /**
     * 修改单个售货机商品信息
     */
    @ApiOperation(value = "修改售货机商品信息")
    @PostMapping("/updateItem")
    @ResponseBody
    @AddLog(operation = "修改售货机商品信息")
    public ApiResponse<String> updateItem(@RequestParam("item_id") Integer item_id,
                                          @RequestParam(value = "item_name",required = false) String item_name,
                                          @RequestParam(value = "specifications",required = false) String specifications,
                                          @RequestParam(value = "price",required = false) Float price,
                                          @RequestParam(value = "item_category_id",required = false) Integer item_category_id,
                                          MultipartFile picture) {

        String imgPath = "#";
        Item item = new Item();
        item.setId(item_id);
        item.setItem_category_id(item_category_id);
        item.setItem_name(item_name);
        item.setPrice(price);
        item.setSpecifications(specifications);
        item.setPicture_url(Utils.upload(picture, imgPath));

        itemService.updateItem(item);

        ApiResponse<String> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.UPDATE_SUCCESS_MESSAGE);

        return response;
    }


    @ApiOperation(value = "售货机商品下架")
    @PostMapping("/offTheShelf")
    @ResponseBody
    @AddLog(operation = "售货机商品下架")
    public ApiResponse<String> offTheShelf(@RequestParam("item_id") Integer item_id) {

        Item item = new Item();
        item.setId(item_id);
        item.setItem_status(0);

        itemService.updateItem(item);

        ApiResponse<String> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage("商品下架成功");

        return response;
    }


    @ApiOperation(value = "售货机商品上架")
    @PostMapping("/onTheMarket")
    @ResponseBody
    @AddLog(operation = "售货机商品上架")
    public ApiResponse<String> onTheMarket(@RequestParam("item_id") Integer item_id) {
        Item item = new Item();
        item.setId(item_id);
        item.setItem_status(1);

        itemService.updateItem(item);

        ApiResponse<String> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage("商品上架成功");

        return response;
    }

    /**
     * 删除指定商品信息

     * @return
     */
    @ApiOperation(value = "删除售货机商品信息")
    @PostMapping("/deleteItem")
    @ResponseBody
    @AddLog(operation = "删除售货机商品信息")
    public  ApiResponse<String> deleteItem(@RequestParam("item_id") Integer item_id) {

        itemService.deleteItem(item_id);

        ApiResponse<String> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.DELETE_SUCCESS_MESSAGE);

        return response;
    }

    @ApiOperation(value = "查询售货机商品信息")
    @ResponseBody
    @GetMapping("/queryItem")
    public ApiResponse<PageInfo<Item>> queryItem(
            String item_name,
            Integer item_category_id,
            Integer item_status,
            Integer pageNum,
            Integer pageSize) {
        ItemCond cond = new ItemCond();
        cond.setItem_category_id(item_category_id);
        cond.setItem_name(item_name);
        cond.setItem_status(item_status);

        PageInfo<Item> items = itemService.queryItemByCondition(cond, pageNum, pageSize);

        ApiResponse<PageInfo<Item>> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(items);
        return response;
    }


    @ApiOperation(value = "根据id获取售货机商品详情")
    @GetMapping({"/getDetail/{id}"})
    @ResponseBody
    public ApiResponse<Item> getItemDetail(@PathVariable Integer id) {
        ApiResponse<Item> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        Item item = itemService.queryOne(id);
        response.setData(item);
        return response;
    }


    @ApiOperation(value = "根据在售货机商品id获取在售商品详情")
    @GetMapping({"/getItemInMachine/{id}"})
    @ResponseBody
    public ApiResponse<ItemInMachine> getItemInMachineDetail(@PathVariable Integer id) {
        ItemInMachine item = itemService.queryItem(id);

        ApiResponse<ItemInMachine> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(item);
        return response;
    }
}
